package com.chiang.canvasdemo2;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new MyView(this));
	}
	
	class MyView extends View{

		private Drawable iDrawable;
		
		public MyView(Context context) {
			super(context);
			init();
		}
		
		public MyView(Context context, AttributeSet attrs) {
			super(context, attrs);
			init();
		}
		
		public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
			super(context, attrs, defStyleAttr);
			init();
		}
		
		private void init(){
			iDrawable = getContext().getResources().getDrawable(R.drawable.balance);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Path path = new Path();
			path.moveTo(50, 0);
			path.lineTo(700, 0);
			path.lineTo(700, 550);
			path.lineTo(550, 700);
			path.lineTo(50, 700); 
			path.lineTo(50, 0); 
			/**
			 * Intersect the current clip with the specified path.
			 */
			canvas.clipPath(path);
			/**
			 * Specify a bounding rectangle for the Drawable. This is where the drawable 
			 * will draw when its draw() method is called.
			 */
			iDrawable.setBounds(0, 0, getWidth(), getHeight()); 
			iDrawable.draw(canvas);
		}
	}
}
